#encoding: utf-8
from flask import Flask,url_for,redirect,render_template
from config import config
import pymysql

app = Flask(__name__)
app.config.from_object(config.DevelopmentConfig)
print "---app start----"


@app.route('/')
def index():
    class Person(object):
        name = u'艳艳'
        age = 18

    p = Person()
    list = url_for('my_list')
    article = url_for('article',id='123')
    username = u'小康'
    data = {
        'username': username,
        'gender': u'男',
        'age': u'18',
        'person': p
    }
    return render_template('index.html',**data)

@app.route('/login/')
def login():
    return '这是登录页面'

@app.route('/article/<id>')
def article(id):
    return u'您请求的参数是：%s' % id

@app.route('/list/')
def my_list():
    return 'list'

@app.route('/question/<is_login>')
def question(is_login):
    if is_login == '1':
        return '这是发布问答页面'
    else:
        return redirect(url_for('login'))

@app.route('/testif/<is_login>')
def testif(is_login):
    if is_login == '1':
        user = {
            'username':u'小康',
            'age': 19
        }
    else:
        user = {}
    return render_template('testif.html', user = user)

@app.route('/testfor/')
def testfor():
    user = {
        'username': u'小康',
        'age': 19
    }
    websites = ['baidu.com','google.com']

    books = [
        {
            'name': u'西游记',
            'author': u'吴承恩',
            'price': 109
        },
        {
            'name': u'红楼梦',
            'author': u'曹雪芹',
            'price': 200
        },
        {
            'name': u'三国演义',
            'author': u'罗贯中',
            'price': 120
        },
        {
            'name': u'水浒传',
            'author': u'施耐庵',
            'price': 130
        }
    ]

    return render_template('testfor.html',user = user,websites = websites,books = books)

@app.route('/wenda/')
def wenda():
    return render_template('wenda.html')

@app.route('/mysql/')
def mysql():
    import flask

    db_config = {
        'host': '172.19.0.3',
        'port': 3306,
        'user': 'root',
        'password': 'xiaokang',
        'db': 'flask',
        'charset': 'utf8',
        'cursorclass': pymysql.cursors.DictCursor,
    }
    db = pymysql.connect(**db_config)
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # 使用 execute()  方法执行 SQL 查询
    cursor.execute("SELECT VERSION()")
    # 使用 fetchone() 方法获取单条数据.
    data = cursor.fetchone()
    # print("Database version : %s " % data)
    # 关闭数据库连接
    db.close()

    return 'mysql version:'+data['VERSION()']+'<br>flask version:'+flask.__version__



if __name__ == "__main__":
    # app.config['DEBUG'] = True
    app.run(host='0.0.0.0')
    # app.run()